package com.chenbei.common.handler;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.chenbei.common.enums.ResponseResultEnum;
import com.chenbei.common.exception.LogicHandleException;
import com.chenbei.common.util.ApiResponseUtil;
import com.chenbei.common.domain.vo.ApiResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(LogicHandleException.class)
    public ApiResponse<Object> loginException(LogicHandleException e){
        return ApiResponseUtil.fail(ResponseResultEnum.LOGIC_HANDLE_EXCEPTION, e.getMessage());
    }

    @ExceptionHandler(NotLoginException.class)
    public ApiResponse<Object> notLoginException(NotLoginException e) {
        return ApiResponseUtil.fail(ResponseResultEnum.USER_AUTHENTICATION_FAILED, e.getMessage());
    }

    @ExceptionHandler(NotPermissionException.class)
    public ApiResponse<Object> notPermissionException(NotPermissionException e) {
        return ApiResponseUtil.fail(ResponseResultEnum.USER_NOT_PERMISSION,"无操作权限");
    }
}
